<?php
namespace app\index\controller;
use PHPMailer\Mail;
use api\Msg;
use api\Robots;
use api\Tq;
use api\Wl;
use app\common\controller\Common;
use app\index\model\Category;
use app\index\model\Reply;
use app\index\model\Signin;
use app\index\model\User;
use think\Controller;
use think\Db;
use think\Hook;
class Index extends Common
{
    public function index()
    {

        // var_dump($_SERVER);

        // // Tq::getTq('西安');
        // // Wl::getinfo();
        // // $res = Msg::send(18925139194,'小明',568978);
        // $res = Robots::chat("广州到北京动车");

        // var_dump($res);


        // exit;
        
        $category = Db::table('fly_category')->order('sort desc')->cache(true,86400)->select();

        $hotQues = Db::table('fly_question')->order('reply_num desc')->limit(10)->cache(true,3600)->select();

        $topQues =  Db::table('fly_question')
        ->alias('t1')
        ->field('t1.*,t2.face,t2.nickname,t3.cname')
        ->join('fly_user t2','t1.uid = t2.uid')
        ->join('fly_category t3','t1.cid=t3.cid')
        ->where(['t1.is_top'=>'1'])
        ->cache(true,3600)
        ->select();

        $where = [];

        if(input('cid')){
            $where['t1.cid'] = input('cid');
        }

        if(!(input('status')===null)){
            $where['t1.status'] = input('status');
        }

         if(input('is_good')){
            $where['t1.is_good'] = input('is_good');
        }

        $allQues =  Db::table('fly_question')
        ->alias('t1')
        ->field('t1.*,t2.face,t2.nickname,t3.cname')
        ->join('fly_user t2','t1.uid = t2.uid')
        ->join('fly_category t3','t1.cid=t3.cid')
        ->order('t1.qid desc')
        ->where($where) 
        ->paginate(3);


        $weekTop = (new Reply)->getWeekTop();

        $this->assign('weekTop',$weekTop);
        $this->assign('allQues',$allQues);
        $this->assign('topQues',$topQues);

        if(input('cid')){
            $this->assign('title',Category::get(input('cid'))->cname);
        }else{
            $this->assign('title',"一个所有老司机都在这里的社区");
        }

        $signInfo = (new Signin)->getSignInfo();

        // var_dump($signInfo)

        $this->assign('hotQues',$hotQues);

        $this->assign('signInfo',$signInfo);

        $this->assign('category',$category);

        return $this->fetch();
    }

    public function sign()
    {

        Hook::listen('sign_int');
        echo 9999;
        exit;
        // if(!session('uid')){
        //     $this->error("你还没有登录，请先登录！");
        // }

        $uid = session('uid');

        $date = date('Y-m-d');

        $signInfo = Signin::get(['uid'=>session('uid')]);


        if($signInfo){

            if($signInfo->sdate==$date){
                $this->error("已经签到，不能重复签到");
            }

            if($signInfo->sdate==date("Y-m-d",strtotime('-1 days'))){

                $signInfo->num +=1;  
            }else{
                $signInfo->num = 1;
            }

            $signInfo->sdate = $date;

            $signInfo->ctime = time();

            $signInfo->save();

            $kiss =  (new Signin)->getKiss($signInfo->num);

            User::get($uid)->setInc('kiss',$kiss);

            $this->success("签到成功");

        }else{
            $data = [
                'uid'=>$uid,
                'num'=>1,
                'ctime'=>time(),
                'sdate'=>$date
            ];
            User::get($uid)->setInc('kiss',5);
            (new Signin)->data($data)->save();
            $this->success("签到成功");
        }

    }
}
